package business

import (
	"github.com/flipped-aurora/gin-vue-admin/server/model/business"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
)

type QuestionnaireSettingApi struct {
}

var questionnaireSettingService = service.ServiceGroupApp.BusinessServiceGroup.QuestionnaireSettingService

// 获取问卷限制设置信息
func (questionnaireSettingApi *QuestionnaireSettingApi) GetQuestionnaireSetting(c *gin.Context) {
	var setting business.QuestionnaireSetting
	err := c.ShouldBindQuery(&setting)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	settingInfo, err := questionnaireSettingService.GetQuestionnaireSetting(setting.QuestionnaireUuid)
	if err != nil {
		response.FailWithMessage("查询失败", c)
		return
	}
	if settingInfo == nil {
		response.FailWithMessage("没有该数据", c)
		return
	}
	response.OkWithData(gin.H{"requestionnaireSetting": settingInfo}, c)
}

// 修改问卷限制设置信息
func (questionnaireSettingApi *QuestionnaireSettingApi) UpdateQuestionnaireSetting(c *gin.Context) {
	var setting business.QuestionnaireSetting
	err := c.ShouldBindJSON(&setting)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	settingMap := map[string]interface{}{
		"phone_is":        setting.PhoneIs,
		"phone_one_is":    setting.PhoneOneIs,
		"phone_SMS_is":    setting.PhoneSMSIs,
		"phone_people_is": setting.PhonePeopleIs,
		"page_is":         setting.PageIs,
	}
	err = questionnaireSettingService.UpdateQuestionnaireSetting(setting.QuestionnaireUuid, settingMap)
	if err != nil {
		response.FailWithMessage("修改失败", c)
		return
	}
	response.OkWithMessage("更新成功", c)
}
